<?php
namespace Admin\Controller;
use Admin\Logic\Controller\MainController;
use Admin\Model\PermissionGroupModel;
use Admin\Model\PermissionModel;

class PermissionController extends MainController {
    /**
     * @var HospitalModel
     */
    protected $permissionGroupModel = null;
    protected $permissionModel = null;
    public function _initialize(){
        $this->permissionGroupModel = new PermissionGroupModel();
        $this->permissionModel = new PermissionModel();
    }
    public function group(){
        $this->assign ( 'list',  $this->permissionGroupModel->group() );
        $this->display();
    }
    
    public function groupEdit(){
        if(!IS_POST){
            $id = I('get.id',null,'int');
            $this->assign ( 'info',$this->permissionGroupModel->groupFind($id) );
            $this->display();
        }else{
            $id = I('id');
            //var_dump($id);exit;
            if(empty($id)){
                $data['group_name'] = I("post.group_name",'','string');
                $res = $this->permissionGroupModel->grouplAdd($data);
                if($res){
                    $this->success('添加成功',U("Permission/group"));
                }else{
                    $this->error('添加失败');
                }
            }else{
                $res = $this->permissionGroupModel->groupUpdate();
                if($res){
                    $this->success("保存成功",U("Permission/group"));
                }else{
                    $this->error('保存失败');
                }                
            }
        }        
    }
    
    public function groupDelete(){
        $id = I('get.id',null,'int');
        if($id){
            $res = $this->permissionGroupModel->groupDel($id);
        }
        if($res){
            $this->success("删除成功");
        }else{
            $this->error("删除失败");
        }
    }

    public function lists(){
        $this->assign ( 'list', $this->permissionModel->lists() );
        $this->display();        
    }
    
    public function edit(){
        if(!IS_POST){
            $id = I('get.id',null,'int');
            $this->assign ( 'info',$this->permissionModel->Find($id) );
            $this->assign ( 'groupinfo',$this->permissionModel->groupinfo() );
            $this->display();
        }else{
            $id = I('id');
            //var_dump($id);exit;
            if(empty($id)){
                
                $data['permissionname'] = I("post.permissionname",'','string');
                $data['url'] = I("post.url",'','string');
                $data['remark'] = I("post.remark",'','string');
                $data['createtime']=time();
                $data['permission_group'] = I("post.permission_group",'','string');
                $res = $this->permissionModel->addList($data);
                if($res){
                    $this->success('添加成功',U("Permission/lists"));
                }else{
                    $this->error('添加失败');
                }
            }else{
                $res = $this->permissionModel->editList();
                if($res){
                    $this->success("保存成功",U("Permission/lists"));
                }else{
                    $this->error('保存失败');
                }                
            }
        }        
    }

    public function delete(){
        $id = I('get.id',null,'int');
        if($id){
            $res = $this->permissionModel->deleteList($id);
        }
        if($res){
            $this->success("删除成功");
        }else{
            $this->error("删除失败");
        }
    }    
}